<?php
/**
 * @package JoiCaddy
 * @copyright Авторские права (C) 2008-2010 Joostina team. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * JoiCaddy - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * JoiCaddy основан на разработках SimpleCaddy от Henk von Pickartz
 */

defined('_VALID_MOS') or die();

class scpaypal {
	var $debugshow="hidden"; // set to text or hidden
	var $paymentsuccess;
	var $paymentfail;
	var $gatewayreturntext;
/**
The products in the cart are defined in the following way
    var $prodcode;  
    var $prodname;
    var $option; 
    var $quantity=1;
    var $unitprice;
    var $formula="";
    var $id="";
    var $caption="";
*/
	function checkout($cart,$ship=null) {
		// this function will use paypal for checkout instead of a standard email to the admin
		// it uses the simple way to upload a cart with contents to paypal as explained here:
		// https://www.paypal.com/IntegrationCenter/ic_standard_home.html#CartUpload
		// data available March, 31 2008.
		
		// Added support for discount products like vouchers results.

		//get PayPal configuration
		$cfg=new sc_configuration();
		$reselleremail=$cfg->get("reselleremail");
		$ppcurrency=$cfg->get("paypalcurrency");
		$ppreturnurl=$cfg->get("ppreturnsuccess");
		$ppreturncancelurl=$cfg->get("ppreturncancel");
		$cfp=$cfg->get ("taxrate");
		$cfp=str_replace("%", "", $cfp);
		if ($cfp>1) {
			$cfp=$cfp/100;
		}
		$taxrate=$cfp;

		if (!$reselleremail) {
			echo "Reseller email is not entered, please add this to the configuration first. PayPal cannot be used!";
			return;
		}

		// add the details to the order
		$gtotal=0; //define the grand total
		$pptax=0; // define the tax for paypal
		if (!$cart) return; // if there is no cart then exit.

		$html = "<form action='https://www.paypal.com/cgi-bin/webscr' method='post' name ='ppform' target='paypal'>";
   		$html .="<input type=\"$this->debugshow\" name=\"cmd\" value=\"_cart\">";
   		$html .="<input type=\"$this->debugshow\" name=\"upload\" value=\"1\">";
   		$html .="<input type=\"$this->debugshow\" name=\"business\" value=\"$reselleremail\">";
		$html .="<input type=\"$this->debugshow\" name=\"currency_code\" value=\"$ppcurrency\">";
		$html .="<input type=\"$this->debugshow\" name=\"rm\" value=\"2\">";
		if($ship['enabled']){
			$html .="<input type=\"$this->debugshow\" name=\"shipping_1\" value=\"".$ship['cost']."\">";
		}
		$fieldnumber=0; // PayPal field numbering variable
		foreach ($cart as $key=>$product) {
            // create a post field and field value for PayPal
            if($product->finalprice>0) { // price positive, so standard product to be paid for
				$fieldnumber = $fieldnumber +1 ; //increment the field number (could also be done with $fieldnumber++)
				$html .= "<input type='$this->debugshow' name='item_name_".$fieldnumber. "' value='".$product->prodname." (".$product->prodcode.") ".$product->option. "'>";
				$html .= "<input type='$this->debugshow' name='amount_".$fieldnumber. "' value='".$product->finalprice. "'>";
				$html .= "<input type='$this->debugshow' name='quantity_".$fieldnumber. "' value='".$product->quantity. "'>";
            }
            else // price <0 so transfer it as a discount amount instead of a product
            {
                $html .= "<input type='$this->debugshow' name='discount_amount_cart' value='".abs($product->finalprice). "'>";
            }
            $gtotal = $gtotal + ($product->finalprice * $product->quantity);
		}
		if ($taxrate>0) {
			$pptax = $gtotal * $taxrate;
		}
		if ($pptax>0) { //either one or both of the taxes have to be applied, so we add the tax field for PP
			$html .= "<input type=\"$this->debugshow\" name=\"tax_cart\" value=\"".number_format($pptax, 2,".", "").'">';//
		}

		$html .="<input type=\"$this->debugshow\" name=\"cancel_return\" value=\"$ppreturncancelurl\">";
		$html .="<input type=\"$this->debugshow\" name=\"return\" value=\"$ppreturnurl\">";

		$html .= SC_WE_USE_PAYPAL;
 		// PayPal requires you use their logo to check out. Check the PayPal site for other button types
 		// look here for more buttons from PayPal https://www.paypal.com/newlogobuttons

/** customizers, do your stuff here!
You may add all kinds of fields now to the paypal "cart" to customize your heading in PayPal and so on.
None of these novelties have been added here, but if you want to customize the appearance of your presence in Paypal,
Here is the place.

*/
 		$html .= '<p><input type="image" name="submit" src="https://www.paypal.com/en_US/i/logo/PayPal_mark_37x23.gif" align="left" style="margin-right:7px;"> <span style="font-size:11px; font-family: Arial, Verdana;">'.SAFER_WAY.'</span><p>';
		// otherwise we could use a simple submit button:
		//$html .='<input type="submit" value="PayPal">  ';
		$html .= "</form>";
		// if you want to add more text here
		// add it here like this:
		// $html .= "Your text here";
		// *before* the line below
		echo $html; // display the html.
	}
}
?>